import re, sys
from Lib import StateDBHelper

AS_PAT = re.compile('^<a.*>AS(\d+)\s*</a>\s*\-*\s*([^-\s][0-9A-Z\-]{2,100}[^-\s])?\s*\-*\s*(.*)$')

print "Loading AS names into DB..."
dbHelper = StateDBHelper.StateDBHelper()
fh = open(sys.argv[1])
for line in fh:
    mo = re.match(AS_PAT, line)
    if mo:
        if (mo.group(2) and len(mo.group(2)) > 60) or (mo.group(3) and len(mo.group(3)) > 200):
            print "'%s' - '%s'(%s): '%s'(%s)" % (mo.group(1), mo.group(2), len(mo.group(2)), mo.group(3), len(mo.group(3)))
            raise
        
        dbHelper.addAS(int(mo.group(1)), mo.group(2), mo.group(3))
        
dbHelper.commit()
print "Done"
    